package com.zyd.shiro.controller.admin;

import java.util.List;
import java.util.Map;

import com.zyd.shiro.business.vo.GoodsConditionVO;
import com.zyd.shiro.business.vo.GoodsEditVO;
import com.zyd.shiro.business.vo.GoodsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zyd.shiro.persistence.beans.Goods;
import com.zyd.shiro.business.service.MallGoodsService;

import com.zyd.shiro.framework.object.PageResult;
import com.zyd.shiro.framework.object.ResponseVO;
import com.zyd.shiro.util.ResultUtil;

/**
 * 控制器层
 *
 * @author RookieWZW
 */
@RestController
@CrossOrigin
@RequestMapping("/goods")
public class GoodsController {

    @Autowired
    private MallGoodsService goodsService;

    /**
     * 查询未被删除的数据
     *
     * @return
     */
    @RequestMapping(value = "/", method = RequestMethod.GET)
    public ResponseVO findAll() {
        List<GoodsVO> goodsVOList = goodsService.findAllByIs_delete(0);
        return ResultUtil.success("查询成功", ResultUtil.tablePage((long) goodsVOList.size(), goodsVOList));

    }

    /**
     * 查询已被删除的数据
     *
     * @return
     */
    @RequestMapping(value = "/deleted", method = RequestMethod.GET)
    public ResponseVO findAllDeleted() {
        List<GoodsVO> goodsVOList = goodsService.findAllByIs_delete(1);
        return ResultUtil.success("查询成功", ResultUtil.tablePage((long) goodsVOList.size(), goodsVOList));

    }

    /**
     * 多条件查询
     *
     * @param searchMap 查询条件封装
     * @return 分页结果
     */
    @RequestMapping(value = "/findSearch", method = RequestMethod.POST)
    public ResponseVO findSearch(Map searchMap) {
        List<GoodsVO> goodsVOList = goodsService.findSearch(searchMap);
        return ResultUtil.success("查询成功", ResultUtil.tablePage((long) goodsVOList.size(), goodsVOList));
    }

    @RequestMapping(value = "/goods", method = RequestMethod.POST)
    public ResponseVO listGoods(GoodsConditionVO vo) {
        List<GoodsVO> goodsVOList = goodsService.voList(vo);
        return ResultUtil.success("查询成功", ResultUtil.tablePage((long) goodsVOList.size(), goodsVOList));
    }

    /**
     * 根据ID查询商品
     *
     * @param id ID
     * @return
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public ResponseVO findOne(@PathVariable Integer id) {
        return ResultUtil.success("查询成功", goodsService.findOne(id));

    }

    /**
     * 修改商品
     *
     * @param goodsEditVO
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
    public ResponseVO update(@RequestBody GoodsEditVO goodsEditVO, @PathVariable Integer id) {


        System.out.println(goodsEditVO.toString());
        for (int i = 0; i < 50; i++) {
            System.out.println("^^^^^^^^^^^^^");
        }
        goodsEditVO.getGoods().setId(id);
        goodsService.update(goodsEditVO);
        return ResultUtil.success("修改成功");
    }

    /**
     * 增加商品
     *
     * @param goodsEditVO
     */
    @RequestMapping(value = "/", method = RequestMethod.POST)
    public ResponseVO add(@RequestBody GoodsEditVO goodsEditVO) {
        goodsService.add(goodsEditVO);
        return ResultUtil.success("增加成功");
    }

    /**
     * 删除
     *
     * @param id
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public ResponseVO delete(@PathVariable Integer id) {
        goodsService.delete(id);
        return ResultUtil.success("删除成功");
    }

    @RequestMapping(value = "/undelete/{id}", method = RequestMethod.GET)
    public ResponseVO unDelete(@PathVariable Integer id) {

        return ResultUtil.success("恢复成功！", goodsService.unDelete(id));
    }

    /***
     * 上架
     * @param id
     * @return
     */
    @RequestMapping(value = "/upStatus/{id}", method = RequestMethod.GET)
    public ResponseVO upStatus(@PathVariable Integer id) {
        goodsService.updateStatus(id, 10);
        return ResultUtil.success("上架成功");
    }

    /***
     * 下架
     * @param id
     * @return
     */
    @RequestMapping(value = "/downStatus/{id}", method = RequestMethod.GET)
    public ResponseVO downStatus(@PathVariable Integer id) {
        goodsService.updateStatus(id, 20);
        return ResultUtil.success("下架成功");
    }


    /***
     *
     * @param searchMap
     * @return
     *前台通过分类进行搜索
     * sortType all price sales
     */

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ResponseVO list(@RequestBody Map searchMap) {
        List<Goods> goodsList = goodsService.findPortal(searchMap);

        return ResultUtil.success("查询成功", goodsList);

    }


}
